package ${model.packagePath};

import com.jfinal.ext.plugin.tablebind.TableBind;
<#if model.hasListImport>
import java.util.List;
import java.util.ArrayList;
import com.jfinal.plugin.activerecord.Db;
</#if>
<#if model.hasDateColumn>
import java.util.Date;
</#if>

@TableBind(tableName = "${model.name}", pkName = "${model.pk}")
public class ${model.name} extends BaseModel<${model.name}> {
	private static final long serialVersionUID = 1L;

	public static ${model.name} dao = new ${model.name}();
	
	<#list model.columns as column>
	public ${column.type} get${column.name?cap_first}() {
		return this.get("${column.name}");
	}
	
	public void set${column.name?cap_first}(${column.type} ${column.name}) {
		this.set("${column.name}", ${column.name});
	}
	
	</#list>
	
    <#list model.finders as finder>
	<#if finder.collection>
	public List<${model.name}> findBy${finder.name}(
	<#list finder.finderColumns as finderColumn>
	${finderColumn.type} ${finderColumn.name}
	<#if finderColumn_has_next>
	,
	</#if>
	</#list>){
		List<${model.name}> result = new ArrayList<${model.name}>();
		StringBuilder sBuilder = new StringBuilder("SELECT * FROM ${model.name} WHERE ");
		<#list finder.finderColumns as finderColumn>
		sBuilder.append("${finderColumn.name} = ?");
		<#if finderColumn_has_next>
		sBuilder.append(" AND ");
		</#if>
		</#list>
		<#if finder.orderColumns?size != 0>
		sBuilder.append(" ORDER BY ");
		</#if>
		<#list finder.orderColumns as orderColumn>
		sBuilder.append("${orderColumn.name} ${orderColumn.orderBy}");
		<#if orderColumn_has_next>
		sBuilder.append(" , ");
		</#if>
		</#list>
		
		result = ${model.name}.dao.find(sBuilder.toString(),
		<#list finder.finderColumns as finderColumn>
		${finderColumn.name}
		<#if finderColumn_has_next>
		,
		</#if>
		</#list>);
		return result;
		}
		
	public int countBy${finder.name}(
	<#list finder.finderColumns as finderColumn>
	${finderColumn.type} ${finderColumn.name}
	<#if finderColumn_has_next>
	,
	</#if>
	</#list>){
		int result = 0;
		StringBuilder sBuilder = new StringBuilder("SELECT COUNT(*) FROM ${model.name} WHERE ");
		<#list finder.finderColumns as finderColumn>
		sBuilder.append("${finderColumn.name} = ?");
		<#if finderColumn_has_next>
		sBuilder.append(" AND ");
		</#if>
		</#list>
		result = Db.queryLong(sBuilder.toString(),
		<#list finder.finderColumns as finderColumn>
		${finderColumn.name}
		<#if finderColumn_has_next>
		,
		</#if>
		</#list>).intValue();
		return result;
		}
	<#else>
	
	public ${model.name} findBy${finder.name}(
	<#list finder.finderColumns as finderColumn>
	${finderColumn.type} ${finderColumn.name}
	<#if finderColumn_has_next>
	,
	</#if>
	</#list>){
		StringBuilder sBuilder = new StringBuilder("SELECT * FROM ${model.name} WHERE ");
		<#list finder.finderColumns as finderColumn>
		sBuilder.append("${finderColumn.name} = ?");
		<#if finderColumn_has_next>
		sBuilder.append(" AND ");
		</#if>
		</#list>
		
		<#if finder.orderColumns?size != 0>
		sBuilder.append(" ORDER BY ");
		</#if>
		<#list finder.orderColumns as orderColumn>
		sBuilder.append("${orderColumn.name} ${orderColumn.orderBy}");
		<#if orderColumn_has_next>
		sBuilder.append(" , ");
		</#if>
		</#list>
		
		${model.name} result = ${model.name}.dao.findFirst(sBuilder.toString(),
		<#list finder.finderColumns as finderColumn>
		${finderColumn.name}
		<#if finderColumn_has_next>
		,
		</#if>
		</#list>);
		return result;
	}
	</#if>
	</#list>
}